package com.netty.websocket;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.ChannelMatchers;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.concurrent.ConcurrentHashMap;

public final class SimpleChannelGroup extends BaseChannelGroup{
    public ConcurrentHashMap<String, ChannelGroup> mapChannelGroup = new ConcurrentHashMap();

    public static ConcurrentHashMap<String,SimpleChannelGroup> mapAllSimpleQueue =new ConcurrentHashMap();//记录所有队列的SingleQueue
    public static SimpleChannelGroup newOrFind(String queue,QueueType queueType) {
        String key=queue+"-"+queueType.name();

        if(mapAllSimpleQueue==null) mapAllSimpleQueue=new ConcurrentHashMap();
        SimpleChannelGroup simpleQueue = mapAllSimpleQueue.get(key);
        if (simpleQueue == null) {
            simpleQueue = new SimpleChannelGroup();
            mapAllSimpleQueue.put(key, simpleQueue);
        }
        return simpleQueue;
    }

    @Override
    public BaseChannelGroup add(NettySession nettySession) {
        if(queue==null)queue=nettySession.getQueue();
        ChannelGroup channelGroup = mapChannelGroup.get(queue);
        if (channelGroup == null) {
            channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
            mapChannelGroup.put(queue, channelGroup);
        }
        channelGroup.add(nettySession.channel());
        mapQueueSize.put(queue,channelGroup.size());
        return this;
    }

    @Override
    public BaseChannelGroup remove(NettySession nettySession) {
        ChannelGroup channelGroup = mapChannelGroup.get(queue);
        if (channelGroup == null) {
            return this;
        }
        mapChannelGroup.remove(nettySession.channel());
        mapQueueSize.put(queue,channelGroup.size());
        return this;
    }

    @Override
    public boolean writeAndFlush(NettySession nettySession,String recvQueue,JSONObject jsonMsg) {
        return writeAndFlush(nettySession,recvQueue,new TextWebSocketFrame(jsonMsg.toJSONString()));
    }

    @Override
    public boolean writeAndFlush(NettySession nettySession,String recvQueue,WebSocketFrame textMessage) {
        ChannelGroup channelGroup=mapChannelGroup.get(queue);//根据队列queeu 获得 ChannelGroup
        if (channelGroup!=null && size()>0) {
            ChannelGroupFuture cgf = channelGroup.writeAndFlush(textMessage, ChannelMatchers.isNot(nettySession.channel()), false);//发送消息的channel不要收到自己发送的消息
            return cgf.isSuccess();
        }
        return false;
    }

    @Override
    public boolean writeAndFlush2(NettySession nettySession,String recvQueue,WebSocketFrame textMessage) {
        ChannelGroup channelGroup=mapChannelGroup.get(queue);//根据队列queeu 获得 ChannelGroup
        if (channelGroup!=null && size()>0) {
            ChannelGroupFuture cgf = channelGroup.writeAndFlush(textMessage);//给队列的所有人发送消息(含自己)
            return cgf.isSuccess();
        }
        return false;
    }
}
